home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / gettimeofday / gettimeofday.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-22  |  2.5 KB  |  87 lines

  1. /* 
  2.  * gettimeofday.c --
  3.  *
  4.  *    This file is a benchmark program to measure the cost of a
  5.  *    minimal kernel call (gettimeofday).  It should be invoked as follows:
  6.  *    
  7.  *    gettimeofday [-n] count
  8.  *
  9.  *    Where count is the number of calls to make.  It makes that
  10.  *    many calls, then prints out the average time per call.  If -n is 
  11.  *    specified, then the gettimeofday calls don't actually ask for the 
  12.  *    time.  The difference between a run with -n and a run without -n 
  13.  *    should be the time to copy the current time-of-day to user space.
  14.  *
  15.  * Copyright 1989 Regents of the University of California
  16.  * Permission to use, copy, modify, and distribute this
  17.  * software and its documentation for any purpose and without
  18.  * fee is hereby granted, provided that the above copyright
  19.  * notice appear in all copies.  The University of California
  20.  * makes no representations about the suitability of this
  21.  * software for any purpose.  It is provided "as is" without
  22.  * express or implied warranty.
  23.  */
  24.  
  25. #ifndef lint
  26. static char rcsid[] = "$Header: /sprite/src/benchmarks/gettimeofday/RCS/gettimeofday.c,v 1.3 92/05/21 20:11:10 kupfer Exp $ SPRITE (Berkeley)";
  27. #endif /* not lint */
  28.  
  29. #include <stdio.h>
  30. #include <sys/time.h>
  31. #include <sys/resource.h>
  32.  
  33. main(argc, argv)
  34.     int argc;
  35.     char **argv;
  36. {
  37.     int count, i;
  38. #ifdef GETRUSAGE
  39.     struct rusage begin ,end;
  40. #endif
  41.     struct timeval start, stop, dummy;
  42.     struct timeval *dummyPtr;
  43.     int micros;
  44.     double timePer;
  45.     char *usage = "Usage: gettimeofday [-n] count\n";
  46.  
  47.     if (argc < 2) {
  48.     fprintf(stderr, usage);
  49.     exit(1);
  50.     }
  51.  
  52.     if (argc > 2 && strcmp(argv[1], "-n") == 0) {
  53.     dummyPtr = NULL;
  54.     --argc;
  55.     ++argv;
  56.     } else if (argc > 2) {
  57.     fprintf(stderr, usage);
  58.     exit(1);
  59.     } else {
  60.     dummyPtr = &dummy;
  61.     }
  62.     count = atoi(argv[1]);
  63.  
  64. #ifdef GETRUSAGE
  65.     getrusage(RUSAGE_SELF, &begin);
  66. #else
  67.     gettimeofday(&start, (struct timezone *) NULL);
  68. #endif
  69.  
  70.     for (i = 0; i < count; i++) {
  71.         (void) gettimeofday(dummyPtr, (struct timezone *) NULL);
  72.     }
  73. #ifdef GETRUSAGE
  74.     getrusage(RUSAGE_SELF, &end);
  75.     micros = (end.ru_utime.tv_sec + end.ru_stime.tv_sec
  76.         - begin.ru_utime.tv_sec - begin.ru_stime.tv_sec)*1000000
  77.         + (end.ru_utime.tv_usec - begin.ru_utime.tv_usec)
  78.         + (end.ru_stime.tv_usec - begin.ru_stime.tv_usec);
  79. #else
  80.     gettimeofday(&stop, (struct timezone *) NULL);
  81.     micros = 1000000*(stop.tv_sec - start.tv_sec)
  82.         + stop.tv_usec - start.tv_usec;
  83. #endif
  84.     timePer = micros;
  85.     printf("Time per iteration: %.2f microseconds\n", timePer/count);
  86. }
  87.